<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Blog;
use app\admin\validate\BlogValidate;

class Index extends Controller {
    public function __construct() {
        parent::__construct();
        if(!session('uid')) {
            $url = url('login/index');
            return header("Location:$url");
        }
    }

    public function index() {
        return $this->lst();
    }

    //日志列表
    public function lst() {
        $blog = new Blog;
        $bloglist = $blog->blogList(session('uid'));
        $this->assign('bloglist',$bloglist);
        return $this->fetch('lst');
    }

    //日志新增
    public function add() {
        if($this->request->isPost()){
            $title = $this->request->post('title','','trim');
            $content = $this->request->post('content','','htmlspecialchars');
            $data = ['title'=>$title,'content'=>$content];
            $validate = new BlogValidate;
            if(!$validate->check($data)) {
                $url = url('index/add');
                header("Refresh:1;url=$url");
                return $validate->getError();
            }else{
                $blog = new Blog;
                if($blog->save(['title'=>$title,'content'=>$content,'save_time'=>time(),'uid'=>session('uid')])){
                    $url = url('index/index');
                    header("Refresh:1;url=$url");
                    return "新增成功";
                }else{
                    $url = url('index/add');
                    header("Refresh:1;url=$url");
                    return "系统错误";
                }
            }
        }else{
            return $this->fetch();
        }
    }

    //日志修改
    public function edit(Blog $blog) {
        if($this->request->isPost()){
            $id = $this->request->post('id');
            $title = $this->request->post('title','','trim');
            $content = $this->request->post('content','','htmlspecialchars');
            $data = ['title'=>$title,'content'=>$content];
            $validate = new BlogValidate;
            if(!$validate->check($data)) {
                $url = url('index/edit','id='.$id);
                header("Refresh:1;url=$url");
                return $validate->getError();
            }else{
                $blogInfo = Blog::get($id);
                $blogInfo->title = $title;
                $blogInfo->content = $content;
                $blogInfo->save_time = time();
                if($blogInfo->save()){
                    $url = url('index/index');
                    header("Refresh:1;url=$url");
                    return "修改成功";
                }else{
                    $url = url('index/edit','id='.$id);
                    header("Refresh:1;url=$url");
                    return "系统错误";
                }
            }
        }else{
            $id = $this->request->route('id');
            $blogInfo = Blog::get($id);
            $this->assign('blogInfo',$blogInfo);
            return $this->fetch();
        }
    }

    //日志删除
    public function del() {
        $id = $this->request->route('id');
        if(Blog::destroy($id)){
            $url = url('index/index');
            header("Refresh:1;url=$url");
            return "删除成功";
        }else{
            $url = url('index/index');
            header("Refresh:1;url=$url");
            return "删除失败";
        }
    }

    //日志详情
    public function show() {
        $id = $this->request->route('id');
        $blogInfo = Blog::get($id);
        $this->assign('blogInfo',$blogInfo);
        return $this->fetch();
    }

    

}
